<?php
defined('BASEPATH') OR exit('No direct script access allowed');
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\IOFactory;



$spreadsheet = new Spreadsheet();
$spreadsheet->getDefaultStyle()->getFont()->setName('微软雅黑');
//$spreadsheet->getDefaultStyle()->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
$spreadsheet->getDefaultStyle()->getAlignment()->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER);
$sheet = $spreadsheet->getActiveSheet()->setTitle(date("Y-m-d").'产品XLSX');
$sheet->getDefaultColumnDimension()->setWidth(24);
$headercolor='FF000000';    
$headerbg='FF92D050';
$field=["产品类别","标题","内容","原价","特价","颜色","尺码","新价格","图片"];

$i=0;
foreach ($field as $key => $value) {
    $sheet->setCellValue(num2alpha($i).'1', $value);
    $sheet->getStyle(num2alpha($i).'1')->applyFromArray(
        ['fill' => [
                    'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
                    'color' => ['argb' => $headerbg],
                ],
        'font' =>[
                'color' => ['argb' => $headercolor]
            ]
        ]
    );
    $i++;
}
$sheet->getRowDimension(1)->setRowHeight(30);


foreach($products as $k=>$p){
   
    $k=$k+1;
    $num=$k+1;
    $i=0;
    $color="";
    $size="";
    $price="";
    $image="";
    $option=unserialize($p["option"]);
    if(empty($option))
    {
        continue;
    }
    $icount=0;
    foreach($option["vv"] as $v)
    {
        $opval="";
        $opname=strtolower($v["name"]);
        foreach($v["value"] as $cc=>$vv)
        {
            $opval.=$vv."|";
            if(isset($option["vc"]) && isset($option["vc"][$icount]))
            {
                $sprice=get_price($price_muti,$option["vc"][$icount]);
            }
            else
            {
                $sprice=get_price($price_muti,$p["price"]);
            }
            if(isset($v["image"]) && isset($v["image"][$cc]))
            {
                $image.=$v["image"][$cc]."|";
            }

            if(strstr($opname,"color")||strstr($opname,"colour"))
            {
                $price.=$sprice."|";
            }
            $icount++;
        }
        
        if(strstr($opname,"color")||strstr($opname,"colour"))
        {
            $color=$opval;
        }
        if(strstr($opname,"size"))
        {
            $size=$opval;
        }

    }
    $v=[$type,$p["name"],$p["desc"],"","",$color,$size,$price,$image];
    foreach($field as $key=>$value){
        $sheet->setCellValue(num2alpha($i++) . $num , $v[$key]);
    }
}
$writer = new Xlsx($spreadsheet);        
$filename=date("Y-m-d")."产品XLSX.xlsx";
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');
$writer->save('php://output');